/**
 * Created by Alice on 2016/9/19 0019.
 */

$(function () {
    var currentDay = utils.get_date(1);
    //查询的开始时间默认给今天
    $('#startTime').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        console.log(ev.date.valueOf());
    });
    $('#endTime').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        console.log(ev.date.valueOf());
    });
    $("#endTime").find("input").val(currentDay);
    $("#conditionBtn").click(function () {
        $("#loading").show();
        loadDownChartsByCourtId();
    });
    $("#sys_court_status").find("a").attr("href", r.base_path + "system_monitor/sys_court_status?courtUid=" + r.court_uid);
    $("#sys_play_info").find("a").attr("href", r.base_path + "system_monitor/sys_play_info?court=Uid" + r.court_uid);
    $("#sys_fault_condition").find("a").attr("href", r.base_path + "views/system_monitor/sys_fault_condition.html?court=" + r.court_id);
    $("#sys_meeting").find("a").attr("href", r.base_path + "views/system_monitor/sys_meeting.html?court=" + r.court_id);
    $("#sys_server_status").find("a").attr("href", r.base_path + "system_monitor/sys_server_status?courtUid=" + r.court_uid);
    $("#sys_warning_message").find("a").attr("href", r.base_path + "system_monitor/sys_warning_message?courtUid=" + r.court_uid);
    $("#sys_equipment_management").find("a").attr("href", r.base_path + "views/system_monitor/sys_equipment_management.html?court=" + r.court_id);
    // $("#loading").hide();
    $('#btn_meeting').attr('href', r.base_path + "views/system_monitor/sys_meeting.html?court=" + r.court_id);
    select_all_child_courts();
    loadDownChartsByCourtId();
    // getSum();
});

function temp(htmlid, id, value) {
    // console.log(id, value);
    $(htmlid).find("button").html(value + '<span class="caret"></span>');
    $(htmlid).find("input").val(id);
}
function getSum() {
    var DataType = $("#dataType").val();
    var SubsysName = $("#subsysName").val();
    var Ip = $("#Ip").val();
    //ip格式的验证
    if (Ip) {
        var regIps = /^(((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|[0-9])\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|[0-9]))$/;
        if (!regIps.test(Ip) && Ip != '') {
            alert("Ip地址不合法");
            $("#loading").hide();
            return;
        }
    }
    var UserId = $("#userName").find("input").val();
    var StartTime = $("#startTime").find("input").val();
    var EndTime = $("#endTime").find("input").val();
    if (StartTime && EndTime) {
        if (StartTime > EndTime) {
            alert("开始时间大于结束时间！");
            $("#loading").hide();
            return;
        }
    }
    $.ajax({
        url: r.base_path + "sys/getSum",
        type: "post",
        data: {
            ip: Ip,
            userId: UserId,
            subsysName: SubsysName,
            logType: DataType,
            startTime: StartTime,
            endTime: EndTime,
            courtId: r.court_id,
            search: r.flag_search
        },
        dataType: "json",
        success: function (result) {
            // console.log(result);
            var sum = result.data;
            if (sum == 0) {
                page(1, 1, null);
            } else {
                page(1, Math.ceil(sum / pageSize), findData);
            }
            findData(0);
        },
        error: function (data) {
            alert("查询失败");
        }

    });
}
function select_all_child_courts() {
    var select_child_courts = $("#selectAllChildCourts");
    select_child_courts.empty();
    $.each(r.court_list, function (i, n) {
        select_child_courts.append("<option value='" + n.uid + "'>" + n.name + "</option>");
    });
    select_child_courts.val(r.court_uid == r.user_court_id ? '' : r.court_uid);
};
function findData(Index) {
    var DataType = $("#dataType").val();
    var SubsysName = $("#subsysName").val();
    var Ip = $("#Ip").val();
    //ip格式的验证
    if (Ip) {
        var regIps = /^(((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|[0-9])\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|[0-9]))$/;
        if (!regIps.test(Ip) && Ip != '') {
            alert("Ip地址不合法");
            $("#loading").hide();
            return;
        }
    }
    var UserId = $("#userName").find("input").val();
    var StartTime = $("#startTime").find("input").val();
    var EndTime = $("#endTime").find("input").val();
    if (StartTime && EndTime) {
        if (StartTime > EndTime) {
            alert("开始时间大于结束时间！");
            $("#loading").hide();
            return;
        }
    }
    $.ajax({
        url: r.base_path + "sys/findLogInfo",
        type: "post",
        data: {
            ip: Ip,
            userId: UserId,
            subsysName: SubsysName,
            logType: DataType,
            startTime: StartTime,
            endTime: EndTime,
            index: Index,
            pageSize: pageSize,
            courtId: r.court_id,
            search: r.flag_search
        },
        dataType: "json",
        success: function (result) {
            console.log(result);
            $("#loading").hide();
            var data = result.data;
            loadDownChartsByCourtId(data);
        },
        error: function (data) {
            alert("查询失败");
        }
    });
}

var page_size = 10;
var starts = 0;
function loadDownChartsByCourtId(page_num) {
    if (r.flag_search) {
        status = 1;
    } else {
        status = 0;
    }
    if (!page_num) {
        page_num = 1
    }
    var courtUid = r.court_uid;
    //搜索框中的法院uid
    if ($("#selectAllChildCourts").val()) {
        courtUid = $("#selectAllChildCourts").val();
    }

    var DataType = $("#dataType").val();
    var SubsysName = $("#subsysName").val();
    var Ip = $("#Ip").val();
    //ip格式的验证
    if (Ip) {
        var regIps = /^(((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|[0-9])\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|[0-9]))$/;
        if (!regIps.test(Ip) && Ip != '') {
            alert("Ip地址不合法");
            $("#loading").hide();
            return;
        }
    }
    var UserId = $("#userName").find("input").val();
    var StartTime = $("#startTime").find("input").val();
    var EndTime = $("#endTime").find("input").val();
    if (StartTime && EndTime) {
        if (StartTime > EndTime) {
            alert("开始时间大于结束时间！");
            $("#loading").hide();
            return;
        }
    }
    $.ajax({
        url: r.base_path + "log/selectLogInfo",
        type: "post",
        data: {
            ip: Ip,
            sysName: SubsysName,
            logType: DataType,
            startDate: StartTime,
            endDate: EndTime,
            starts: starts,
            page: page_num,
            pageSize: page_size,
            courtUid: courtUid
        },
        dataType: "json",
        success: function (result) {
            // console.log(result)
            var countTotal = result.data.count;

            if (countTotal <= 0) {

                page(1, 1, null);
            } else {
                page(page_num, Math.ceil(countTotal / page_size), loadDownChartsByCourtId);
            }
            $("#loading").hide();
            $('#tableDivId').empty();
            var tr = ' <table id="table" class="table table-bordered text-center" ></table>';
            $('#tableDivId').append(tr);
            for (var j = 0; j < result.length; j++) {
                var lt = result[j].logType;
                if (lt = 2) {
                    result[j].logType = "信息";
                }
                var st = result[j].logDate.substring(0, 11);
                var split = result[j].logtime.split(":");
                var dateTime = st + " " + result[j].logtime;
                result[j].logDate = dateTime;
            }
            $('#table').bootstrapTable({
                paginationLoop: true,
                columns: [{
                    field: 'logType',
                    title: '日志类型'
                }, {
                    field: 'dateCreated',
                    formatter: function (value, row, index) {
                        var date = new Date(value);
                        var month = date.getMonth() + 1;
                        var day = date.getDay();
                        ;
                        if ((date.getMonth() + 1) < 10) {
                            month = "0" + (date.getMonth() + 1);
                        }
                        if (date.getDay() < 10) {
                            day = "0" + date.getDay();
                        }
                        return date.getFullYear() + "-" + month + "-" + day;
                    },
                    title: '日期时间'
                }, {
                    field: 'sysName',
                    title: '日志系统'
                }, {
                    field: 'title',
                    title: '来源'
                }, {
                    field: 'ip',
                    title: '计算机'
                }
                ],
                data: result.data.logList
            })
        },
        error: function (data) {
            $("#loading").hide();
            alert("查询失败");
        }
    });
}
function page(now, totalPages, callback) {
    base.pagination('#_page', {
        currentPage: now,
        totalPages: totalPages,
        onPageClicked: function (page) {
            callback(page);
        }
    });
}